<?php
namespace app\admin\controller;
use think\Db;
class Suggest extends Base
{
    public function index()
    {
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['f_type'] = $role;
            }
            if(session ('area')){
                $map['f_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['f_content'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('feedback')
                ->where($map)
                ->count();
            $lists = Db::name('feedback')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('f_add_time desc')
                ->select ();
            if(!empty($lists)){
                foreach($lists as $key=>$vo){
                    if($vo['f_type'] == 1){
                        $data = Db::name('u_user')->where(['user_id'=>$vo['f_user_id']])->find();
                        $area = Db::name('area')->where('district_id',$data['user_area'])->value('district');
                        $lists[$key]['name'] = $data['user_username'];
                        $lists[$key]['area'] = $area;
                    }else{
                        $data = Db::name('shop')->where(['shop_id'=>$vo['f_user_id']])->find();
                        $area = Db::name('area')->where('district_id',$data['shop_qu_id'])->value('district');
                        $lists[$key]['name'] = $data['shop_name'];
                        $lists[$key]['area'] = $area;
                    }
                    $lists[$key]['f_images'] = trim_explode (',',$vo['f_images']);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ("suggest/index");
    }


    public function changeStatus(){
        extract (input());
        $res = Db::name('feedback')->where('f_id',$id)->setField ('f_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'处理成功']);
        }else{
            return json(['code'=>100,'msg'=>'处理失败']);
        }
    }
}